import os
import re

def rename_files(directory):
    # 获取目录中的所有文件
    files = os.listdir(directory)

    # 遍历所有文件
    for filename in files:
        # 匹配文件名是否符合 `S11867M1-80tr3-tc32.png` 格式
        match = re.match(r'^(.*?)(tr\d+-tc\d+\.png)$', filename)
        if match:
            prefix, suffix = match.groups()
            # 新文件名为 `S11867M1-80_tr3-tc32.png`
            new_filename = f"{prefix}_{suffix}"
            # 获取旧文件的完整路径
            old_filepath = os.path.join(directory, filename)
            # 获取新文件的完整路径
            new_filepath = os.path.join(directory, new_filename)
            # 重命名文件
            os.rename(old_filepath, new_filepath)
            print(f"Renamed: {filename} -> {new_filename}")

# 示例用法
directory = '/root/data/deep_align/11867_32nm_to_8nm_fine_align'  # 替换为实际目录路径
rename_files(directory)
